package com.trade.data.manager;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.trade.data.dao.entity.PredictInfo;
import com.trade.data.dao.service.PredictInfoService;
import com.trade.data.dto.PredictInfoDto;
import com.trade.data.model.ModelEnum;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class PredictManager {
    @Autowired
    PredictInfoService predictInfoService;

    public PredictInfoDto getPredictResult(String coin, String interval) {
        // 当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        LocalDateTime localDateTimeAdd = addTime(currentTime, interval);
        LocalDateTime localDateTimePlus = plusTime(currentTime, interval);
        List<PredictInfo> list = predictInfoService.list(new LambdaQueryWrapper<PredictInfo>().
                eq(PredictInfo::getCurrencyName, coin).eq(PredictInfo::getPeriodDimension,
                        interval).ge(PredictInfo::getPredictClosetime,localDateTimePlus).le(PredictInfo::getPredictClosetime,localDateTimeAdd)
                .orderByDesc(PredictInfo::getPredictCreateat)
                .orderByDesc(PredictInfo::getPredictClosetime));
        PredictInfoDto predictInfoDto = new PredictInfoDto();
        if(!CollectionUtils.isEmpty(list)){
            BeanUtil.copyProperties(list.get(0),predictInfoDto);
        }
       return predictInfoDto;
    }

    private LocalDateTime addTime(LocalDateTime currentTime, String periodValue) {
        switch (periodValue) {
            case "1m":
                return currentTime.plusMinutes(1);
            case "3m":
                return currentTime.plusMinutes(3);
            case "5m":
                return currentTime.plusMinutes(5);
            case "15m":
                return currentTime.plusMinutes(15);
            case "30m":
                return currentTime.plusMinutes(30);
            case "1h":
                return currentTime.plusHours(1);
            case "2h":
                return currentTime.plusHours(2);
            case "4h":
                return currentTime.plusHours(4);
            case "6h":
                return currentTime.plusHours(6);
            case "8h":
                return currentTime.plusHours(8);
            case "12h":
                return currentTime.plusHours(12);
            case "1d":
                return currentTime.plusDays(1);
            case "3d":
                return currentTime.plusDays(3);
            case "1w":
                return currentTime.plusWeeks(1);
            case "1M":
                return currentTime.plusMonths(1);
            default:
                throw new IllegalArgumentException("Unsupported period value: " + periodValue);
        }
    }
    private LocalDateTime plusTime(LocalDateTime currentTime, String periodValue) {
        switch (periodValue) {
            case "1m":
                return currentTime.plusMinutes(-1);
            case "3m":
                return currentTime.plusMinutes(-3);
            case "5m":
                return currentTime.plusMinutes(-5);
            case "15m":
                return currentTime.plusMinutes(-15);
            case "30m":
                return currentTime.plusMinutes(-30);
            case "1h":
                return currentTime.plusHours(-1);
            case "2h":
                return currentTime.plusHours(-2);
            case "4h":
                return currentTime.plusHours(-4);
            case "6h":
                return currentTime.plusHours(-6);
            case "8h":
                return currentTime.plusHours(-8);
            case "12h":
                return currentTime.plusHours(-12);
            case "1d":
                return currentTime.plusDays(-1);
            case "3d":
                return currentTime.plusDays(-3);
            case "1w":
                return currentTime.plusWeeks(-1);
            case "1M":
                return currentTime.plusMonths(-1);
            default:
                throw new IllegalArgumentException("Unsupported period value: " + periodValue);
        }
    }

    public List<PredictInfoDto> queryCoinPeriodStrategy(String coin, String period, ModelEnum modelEnum){
        List<PredictInfoDto> result = Lists.newArrayList();
        List<PredictInfo> list = predictInfoService.list(new LambdaQueryWrapper<PredictInfo>().
                eq(PredictInfo::getCurrencyName, coin).eq(PredictInfo::getPeriodDimension,
                        period).eq(PredictInfo::getModelName,modelEnum.name())
                .orderByDesc(PredictInfo::getPredictCreateat)
                .orderByDesc(PredictInfo::getPredictClosetime));
        PredictInfoDto predictInfoDto = new PredictInfoDto();
        if(!CollectionUtils.isEmpty(list)){
            result = BeanUtil.copyToList(list,PredictInfoDto.class);
        }
        return result;
    }
}
